# https://www.aqistudy.cn/historydata/
import requests
from lxml import etree
# url = 'https://www.aqistudy.cn/historydata/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
page_text = requests.get(url,headers).text
tree = etree.HTML(page_text)
hot_city_list = tree.xpath('/html/body/div[3]/div/div[1]/div[1]/div[2]/ul/li')
all_city_list = tree.xpath('/html/body/div[3]/div/div[1]/div[2]/div[2]//li')
with open('./city.txt','a',encoding='utf-8') as f:
    f.write('热门城市：'+'\n')
    for each_city1 in hot_city_list:
        hot_city_name = each_city1.xpath('./a/text()')[0]
        f.write(hot_city_name+' ')
        print(hot_city_name,"写入成功")
    f.write('\n'+'全部城市：'+'\n')
    for each_city2 in all_city_list:
        all_city_name = each_city2.xpath('./a/text()')[0]
        f.write(all_city_name+' ')
        print(all_city_name, "写入成功")
print("全部写入完成！")


